//
//  JMIPAlbumsViewController.m
//  ImagePicker
//
//  Created by Jamin on 2018/10/15.
//  Copyright © 2018 jamin. All rights reserved.
//

#import "JMIPAlbumsViewController.h"
#import "JMPhotosManager.h"
#import "JMIPAblumCell.h"
#import "JMIPAssetsViewController.h"
#import "JMImagePickerDefines.h"
#import "NSBundle+JMIP.h"

#import "JMImagePickerViewController.h"


@interface JMIPAlbumsViewController () <
UITableViewDelegate,
UITableViewDataSource>


@property (nonatomic, strong) UIBarButtonItem *doneButton;
@property (nonatomic, strong) UIBarButtonItem *cancelButton;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray <JMIPAlbum *> *albums;


@property (nonatomic, readonly) JMImagePickerViewController *picker;

@end

@implementation JMIPAlbumsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = JMIPLocatizedString(@"Photos");
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupButtons];
    [self.view addSubview:self.tableView];
    __weak typeof(self) ws = self;
    if ( [JMPhotosManager authorizationStatusAuthorized]) {
        [JMPhotosManager getAlbumListWithcompletion:^(NSArray<JMIPAlbum *> *albums) {
            ws.albums = albums;
            [ws.tableView reloadData];
        }];
    } else {
        [JMPhotosManager requestAuthorization:^{
            [JMPhotosManager getAlbumListWithcompletion:^(NSArray<JMIPAlbum *> *albums) {
            }];
        }];
    }
}

- (JMImagePickerViewController *) picker {
    return (JMImagePickerViewController *)self.navigationController;
}


- (void)setupButtons
{
    if (self.doneButton == nil) {
        NSString *title = JMIPLocatizedString(@"Done"); //(self.picker.doneButtonTitle) ? self.picker.doneButtonTitle : JMIPLocatizedString(@"Done");
        
        self.doneButton =
        [[UIBarButtonItem alloc] initWithTitle:title
                                         style:UIBarButtonItemStyleDone
                                        target:self.picker
                                        action:@selector(finishPickingAssets:)];
    }
    
    if (self.cancelButton == nil) {
        self.cancelButton =
        [[UIBarButtonItem alloc] initWithTitle:JMIPLocatizedString(@"Cancel")
                                         style:UIBarButtonItemStylePlain
                                        target:self.picker
                                        action:@selector(dismiss:)];
        self.navigationItem.rightBarButtonItem = self.cancelButton;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.albums.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMIPAblumCell *cell = [tableView dequeueReusableCellWithIdentifier:[JMIPAblumCell description]];
    if (cell) {
        JMIPAlbum *ablum = self.albums[indexPath.row];
        cell.titleLabel.text = ablum.name;
        cell.subtitleLabel.text = [NSString stringWithFormat:@"(%zi)", ablum.count];
        [JMPhotosManager getAblumThumbnailImage:ablum completion:^(UIImage *image) {
            cell.coverImageView.image = image;
        }];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JMIPAssetsViewController *assetVC = [JMIPAssetsViewController new];
    assetVC.ablum = self.albums[indexPath.row];
    [self.navigationController pushViewController:assetVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[JMIPAblumCell class] forCellReuseIdentifier:[JMIPAblumCell description]];
        _tableView.rowHeight = 64.0;
    }
    return _tableView;
}


@end
